/*
 * CheckLogin.java
 * 
 * Version information
 * 
 * Apr 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.ilink.webapp.struts.action;

import static net.amsoft.iservice.util.IServiceConstants.*;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.ilink.webapp.dataObject.LoginData;
import net.amsoft.iservice.ilink.service.ILinkService;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Action class is used to check if user is logged in or not, if it is then
 * takes him to his homepage otherwise again shows the iname page.
 * 
 * @author mohit
 * 
 */
public class CheckLogin extends BaseAction {
    private static final Logger oLogger = Logger.getLogger(CheckLogin.class);

    public ActionForward process(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
            throws Exception {
        oLogger.info("process() : entry");

        // Check if request is for forwarding service
        String sXri = oRequest.getParameter("xri");
        if (sXri != null) {

            /*
             * Normalize requested XRI
             */
            sXri = sXri.replaceAll("\\s", "+");
            String sIname = null;
            try {
                sIname = IServiceUtil.getIName(sXri);
                if (sIname == null)
                    throw new Exception("Invalid XRI");
                else
                    sIname = sIname.toLowerCase();
            } catch (Exception e) {
                oLogger.warn("process(): invalid xri:" + sXri);
                return oMapping.findForward(ILinkConstants.FWD_INAME_PAGE);
            }

            /*
             * CHECK IF THE SERVICE IS ACTIVE OR NOT for the given synonym and
             * respond
             */
            String sAcceptHeader = oRequest.getParameter(PARAM_SERVICE_ACTIVE);
            if (sAcceptHeader != null && sAcceptHeader.equals("true")) {

                boolean bIsSubscribed = ILinkService.verifySynonym(oRequest
                        .getServerName(), sIname);
                if (bIsSubscribed) {
                    oResponse.setStatus(HttpServletResponse.SC_OK); // HTTP 200

                    // Write response back to client
                    oResponse.setContentType(RESPONSE_URI);
                    PrintWriter out = oResponse.getWriter();
                    out.println("http://" + oRequest.getServerName()
                            + "/ilink/Login?synonym=" + sIname);
                    out.flush();
                    out.close();
                } else {
                    oResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); // HTTP
                    // 404
                }

                return new ActionForward();
            }

            /*
             * Forward to Forward action
             */
            sIname = URLEncoder.encode(sIname, "UTF-8");
            ActionForward oActionForward = new ActionForward("/Forward"
                    + "?xri=" + sXri, true);
            return oActionForward;
        }

        LoginData oLoginData = (LoginData) oRequest.getSession(false)
                .getAttribute(ILinkConstants.SESSION_LOGIN_OBJ);

        if (oLoginData.isLoginStatus()) {
            oLogger.info("process() : forward to welcome page");
            return oMapping.findForward(ILinkConstants.FWD_SUCCESS);
        } else {
            oLogger.info("process() : forward to iname page");
            return oMapping.findForward(ILinkConstants.FWD_INAME_PAGE);
        }
    }
}
